import com.util.DictUtil;
            
class TeleBookController 
{
    def jdbcTmplt;
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [save:'POST', update:'POST']

    def list = 
    {
    	def flag = params.flag;
        if(!params.max) params.max = DictUtil.PARAMSMAX;
        if(!params.offset) params.offset = 0;
        def nametj = "";
        def teleBookInstanceList = [];
        def totalRow;   
        if(params.nametj)
        	nametj = params.nametj;
        if(nametj && nametj != "")
        {
        	if(params.sort && params.order)
        	{
        		teleBookInstanceList = TeleBook.findAll("from TeleBook where realname like '%${nametj}%' and flag='${params.flag}' order by ${params.sort} ${params.order}", [max:params.max.toInteger(), offset:params.offset.toInteger()]);
        	}else
        	{
        		teleBookInstanceList = TeleBook.findAll("from TeleBook where realname like '%${nametj}%' and flag='${params.flag}'", [max:params.max.toInteger(), offset:params.offset.toInteger()]);
        	}
        	totalRow = TeleBook.executeQuery("select count(*) from TeleBook where realname like '%${nametj}%' and flag='${params.flag}'");
        	totalRow = totalRow[0];       	
        }else
        {
        	if(params.sort && params.order)
        	{
        		teleBookInstanceList = TeleBook.findAll("from TeleBook where flag='${params.flag}' order by ${params.sort} ${params.order}", [max:params.max.toInteger(), offset:params.offset.toInteger()]);
        	}else
        	{
        		teleBookInstanceList = TeleBook.findAll("from TeleBook where flag='${params.flag}'", [max:params.max.toInteger(), offset:params.offset.toInteger()]);
        	}
        	totalRow  = TeleBook.executeQuery("select count(*) from TeleBook where flag='${params.flag}'");
        	totalRow = totalRow[0];
        }
        
        return [ teleBookInstanceList:teleBookInstanceList, totalRow:totalRow, nametj:nametj, max:params.max, offset:params.offset, sort:params.sort, order:params.order, flag:flag ]
    }
    

    def show = 
    {
        def teleBookInstance = TeleBook.get( params.id )

        if(!teleBookInstance) 
        {
            flash.message = message(code:'lxh.display.noFindMsg')
            redirect(action:list)
        }
        else { return [ teleBookInstance : teleBookInstance ] }
    }

    def delete = 
    {
        def teleBookInstance = TeleBook.get( params.id )
        if(teleBookInstance) 
        {
            teleBookInstance.delete()
            flash.message = message(code:'lxh.display.deleteMsg')
            redirect(action:list, params:[flag:params.flag])
        }else 
        {
            flash.message = message(code:'lxh.display.noFindMsg')
            redirect(action:list, params:[flag:params.flag])
        }
    }

    def edit = 
    {
        def teleBookInstance = TeleBook.get( params.id )

        if(!teleBookInstance) 
        {
            flash.message = message(code:'lxh.display.noFindMsg')
            redirect(action:list, params:[flag:params.flag])
        }else 
        {
            return [ teleBookInstance : teleBookInstance, flag:params.flag ]
        }
    }

    def update = 
    {
        def teleBookInstance = TeleBook.get( params.id )
        if(teleBookInstance) 
        {
            teleBookInstance.properties = params
            if(!teleBookInstance.hasErrors() && teleBookInstance.save()) 
            {
                flash.message = message(code:'lxh.display.updateMsg')
                redirect(action:list, params:[flag:params.flag])
            }else 
            {
                render(view:'edit',model:[teleBookInstance:teleBookInstance,flag:params.flag])
            }
        }else 
        {
            flash.message = "TeleBook not found with id ${params.id}"
            redirect(action:edit,id:params.id, params:[flag:params.flag])
        }
    }

    def create = 
    {
        def teleBookInstance = new TeleBook()
        teleBookInstance.properties = params
        return ['teleBookInstance':teleBookInstance, flag:params.flag]
    }

    def save = 
    {
        def teleBookInstance = new TeleBook(params)
        teleBookInstance.flag = params.flag;
        if(!teleBookInstance.hasErrors() && teleBookInstance.save()) 
        {
            flash.message = message(code:'lxh.display.createMsg')
            redirect(action:list, params:[flag:params.flag])
        }else 
        {
            render(view:'create',model:[teleBookInstance:teleBookInstance,flag:params.flag])
        }
    }
    
    def delBatch = 
    {
    	def batchIds = params.batchIds;
    	jdbcTmplt.update("delete from Tele_Book where id in (${batchIds})");
    	redirect(action:list, params:[flag:params.flag]);
    }
}